package com.icoolkj.mall.account.controller;

import com.icoolkj.mall.account.dto.AccountRequest;
import com.icoolkj.mall.account.service.AccountService;
import com.icoolkj.mall.common.Result;
import com.icoolkj.mall.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/15 20:26
 */
//@CrossOrigin
@RestController
@RequestMapping("/api/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/reduce-balance")
    public Result<?> reduceBalance(@RequestBody AccountRequest accountRequest) {
        try {
            accountService.reduceBalance(accountRequest.getUserId(), accountRequest.getOrderCost());
        }
        catch (BusinessException e) {
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @GetMapping("/get-balance-account")
    public Result<?> getBalanceAccount(@RequestParam("userId") Long userId) {
        return accountService.getBalanceAccount(userId);
    }

}
